package com.anze.abd.ui.product;

import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.anze.abd.persistence.domain.Product;
import com.anze.abd.persistence.domain.Stroke;
import com.anze.abd.service.product.ProductService;
import com.anze.abd.service.stroke.StrokeService;
import com.anze.base.ui.commons.BaseController;
import com.anze.base.utils.BeanUtils;
import com.anze.base.utils.PageNavInfoUtils;

@Controller
@RequestMapping(value="product")
public class ProductController extends BaseController{
	
	@Resource(name = "productService")
	private ProductService productService;
	@Resource(name = "strokeService")
	private StrokeService strokeService;
	/**
	 * 判断是否存在产品
	 * @param id
	 * @return
	 */
	public Product validProduct(String id){
		
		if(id!=null){
			
			long id1 =Long.parseLong(id);
			Product product=productService.get(id1);
			
			return product;
		}else {
			
			Product product=new Product();
			
			return product;
		}
	}
	
	/**
	 * 描述：列表
	 * @return
	 */
	@RequestMapping()
	public ModelAndView list() {
		
		PageNavInfoUtils.setPageNavInfo();
		return new ModelAndView("/product/product_list");
	}
	
	/**
	 * 描述：保存新增
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public @ResponseBody String save(HttpServletRequest request) {
		
		Product product = formConverter.convertToBean(request, Product.class);		
		Stroke stroke=formConverter.convertToBean(request, Stroke.class);
		//将不能一键填充的数据手动填充
		String [] car=request.getParameterValues("car[]");	
		stroke.setCar(num(car));
		
		List<Stroke> strokes=new ArrayList<>();
		strokes.add(stroke);
		product.setStrokes(strokes);

		String [] requir=request.getParameterValues("requir[]");
		product.setRequir(num(requir));
		
		String [] passenger=request.getParameterValues("passenger[]");
		product.setPassenger(num(passenger));
		
		productService.save(product);
		
		return "true";
	}
	
	/**
	 * 计算哪些radioBox被勾选
	 * @param s
	 * @return
	 */
	private int num(String [] s){
		if(s!=null){
			
			//获取被选取的radioBox
			int i=0;
			for(int j=0;j<s.length;j++)
			{
				i+= Integer.parseInt(s[j]);
			}
			return i;
		}
		return -1;
	}
	
	/**
	 * 描述：保存更新
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public @ResponseBody String update(HttpServletRequest request) throws IllegalArgumentException, IllegalAccessException, InvocationTargetException {
		
		Long id = Long.parseLong(request.getParameter("id"));
		Product product = productService.get(id);
		Long strokeId = product.getStrokes().get(0).getId();
		Stroke stroke =strokeService.get(strokeId);
		
		Product product1 = formConverter.convertToBean(request, Product.class);
		Stroke stroke1=formConverter.convertToBean(request, Stroke.class);
		//将不能一键填充的数据手动填充
		String [] car=request.getParameterValues("car[]");	
		stroke1.setCar(num(car));
		
		List<Stroke> strokes=new ArrayList<>();
		stroke1.setId(strokeId);
		BeanUtils.copyNotNullProperties(stroke, stroke1, new String[]{});
		
		strokes.add(stroke);
		product1.setStrokes(strokes);

		String [] requir=request.getParameterValues("requir[]");
		product1.setRequir(num(requir));
		
		String [] passenger=request.getParameterValues("passenger[]");
		product1.setPassenger(num(passenger));
		
		BeanUtils.copyNotNullProperties(product, product1, new String[]{});
		System.out.println(gson.toJson(product));
		
		productService.update(product);
		return "true";
	}
	
	/**
	 * 描述：保存删除
	 * @param request
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "del")
	public @ResponseBody String del(HttpServletRequest request, @RequestParam Long id) {
		System.out.println(12);
		productService.delete(id);
		return "true";
	}
	
	/**
	 * 接受ajax请求，返回查询数据
	 */
	@RequestMapping(value = "querryById", method = RequestMethod.POST)
	public @ResponseBody String  querry(HttpServletRequest request, HttpServletResponse response){
	    Long id = Long.parseLong(request.getParameter("id"));
	    Product product = productService.get(id);
	 return   gson.toJson(product);
	}
}
